﻿using System.Collections.Generic;
using System.Linq;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Business
{
    public partial class Business
    {

        #region -- Get Capacity --

        /// <summary>
        ///  This function shows list of capacities in major.
        /// </summary>
        /// <param name="idMajor"> Id of major</param>
        /// <returns></returns>
        public List<Capacity> GetListCapacityByMajor(int idMajor)
        {
            return DataContex != null ? DataContex.Capacities.Where(c => c.IdMajor == idMajor).ToList() : null;
        }

        #endregion

        #region Common

        private bool CapacityCommonValidator(Capacity capacity, out string message)
        {
            if (capacity.MaxCredit < capacity.MinCredit)
            {
                message = string.Format(ConstantMessages.CapacityInvalideData);
                return false;
            }

            if (capacity.MaxCredit < 0)
            {
                message = string.Format(ConstantMessages.DataUnderMinValue, "Số tín chỉ tối đa");
                return false;
            }

            if (capacity.MinCredit < 0)
            {
                message = string.Format(ConstantMessages.DataUnderMinValue, "Số tín chỉ tối thiểu");
                return false;
            }

            if (capacity.IdCourse == 0 || !DataContex.Courses.Any(c => c.Id == capacity.IdCourse))
            {
                message = string.Format(ConstantMessages.CannotCreateWhenParentNotExist, "Giới hạn tín chỉ", "Kỳ");
                return false;
            }

            if (capacity.IdMajor == 0 || !DataContex.Majors.Any(m => m.Id == capacity.IdMajor))
            {
                message = string.Format(ConstantMessages.CannotCreateWhenParentNotExist, "Giới hạn tín chỉ", "Ngành");
                return false;
            }

            message = string.Empty;
            return true;
        }


        #endregion

        #region --Edit Capacity--

        /// <summary>
        /// This fuction check validate of edit a capacity function 
        /// </summary>
        /// <param name="capacity"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool EditCapacityValidator(Capacity capacity, out string message)
        {
            var id = capacity.Id;
            if (!DataContex.Capacities.Any(c => c.Id == id))
            {
                message = ConstantMessages.CanNotIdentifyItem;
                return false;
            }

            return CapacityCommonValidator(capacity, out message);
        }

        /// <summary>
        /// This fuction edits a capacity
        /// </summary>
        /// <param name="capacity"></param>
        public bool EditCapacity(Capacity capacity)
        {
            var id = capacity.Id;
            var updateCapacity = DataContex.Capacities.SingleOrDefault(c => c.Id == id);

            if (updateCapacity == null)
                return false;

            updateCapacity.MaxCredit = capacity.MaxCredit;
            updateCapacity.MinCredit = capacity.MinCredit;
            updateCapacity.IdMajor = capacity.IdMajor;
            updateCapacity.IdCourse = capacity.IdCourse;
            DataContex.SaveChanges();
            return true;
        }

        #endregion
    }
}